package pe.gob.essalud.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@PropertySources(value = { @PropertySource("classpath:/application.properties") })
@EnableTransactionManagement
@EnableJpaRepositories("pe.gob.essalud.dao") 
public class PersistenceConfig {

	@Value("${hibernate.generateDdl}")
	private Boolean KEY_GENERATE_DDL;

	@Value("${hibernate.showSql}")
	private Boolean KEY_SHOW_SQL;

	@Value("${entity.package}")
	private String KEY_ENTITIES_PKG;

	@Value("${hibernate.dialect}")
	private String KEY_DIALECT;

	@Value("${hibernate.temp.use_jdbc_metadata_defaults}")
	private String KEY_TEMP_USE_JDBC_METADATA_DEFAULTS;

	@Value("${hibernate.formatSql}")
	private String KEY_FORMAT_SQL;

	@Value("${jdbc.driverClass}")
	private String KEY_DRIVER_CLASS;

	@Value("${jdbc.url}")
	private String KEY_JDBC_URL;

	@Value("${jdbc.username}")
	private String KEY_JDBC_USERNAME;

	@Value("${jdbc.password}")
	private String KEY_JDBC_PASSWORD;

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Bean
	public PlatformTransactionManager transactionManager(
			LocalContainerEntityManagerFactoryBean entityManagerFactory) {
		EntityManagerFactory factory = entityManagerFactory.getObject();
		return new JpaTransactionManager(factory);
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
		HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
		// vendorAdapter.setGenerateDdl(KEY_GENERATE_DDL);
		// vendorAdapter.setShowSql(KEY_SHOW_SQL);
		factory.setDataSource(dataSource());
		factory.setJpaVendorAdapter(vendorAdapter);
		factory.setPackagesToScan(KEY_ENTITIES_PKG);

		Properties jpaProperties = new Properties();
		jpaProperties.setProperty("hibernate.dialect", KEY_DIALECT);
		jpaProperties.setProperty("hibernate.temp.use_jdbc_metadata_defaults",
				KEY_TEMP_USE_JDBC_METADATA_DEFAULTS);
		// jpaProperties.setProperty("hibernate.formatSql", KEY_FORMAT_SQL);

		factory.setJpaProperties(jpaProperties);

		return factory;
	}

	@Bean
	public HibernateExceptionTranslator hibernateExceptionTranslator() {
		return new HibernateExceptionTranslator();
	}

	@Bean
	public DataSource dataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(KEY_DRIVER_CLASS);
		dataSource.setUrl(KEY_JDBC_URL);
		dataSource.setUsername(KEY_JDBC_USERNAME);
		dataSource.setPassword(KEY_JDBC_PASSWORD);
		return dataSource;
	}

}
